package com.spudtech.collision
{
    
    import flash.geom.*;
    
    import com.spudtech.*;
    
    
    public class SpudCollider
    {
        
        public var x : Number;
        public var y : Number;
        
        public function SpudCollider(position : Point = null)
        {
            if (position == null)
            {
                position = new Point(0,0);
            }
            
            this.x = position.x;
            this.y = position.y;
            
        }
        
        
        public function containsPoint(point : Point) : Boolean
        {
            // Implement in derived classes
            return false;
        }
        
        public function overlaps(other : SpudCollider) : Boolean
        {
            // Implement in derived classes
            return false;
        }
        
        public function overlapAnyList(others : Array) : Boolean
        {
            var is_collider : Function = function(item : *, index : int, array : Array) : Boolean
            {
                if (item is SpudCollider)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            
            for each (var coll : SpudCollider in others.filter(is_collider))
            {
                if (overlaps(coll))
                {
                    // We overlap with one
                    // Return true
                    return true;
                }
            }
            
            // We didn't overlap with any
            return false;
            
        }
        
        public function overlapAllList(others : Array) : Boolean
        {
            var is_collider : Function = function(item : *, index : int, array : Array) : Boolean
            {
                if (item is SpudCollider)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            
            for each (var coll : SpudCollider in others.filter(is_collider))
            {
                if (!(overlaps(coll)))
                {
                    // We don't overlap with one
                    // Return false
                    return false;
                }
            }
            
            // We overlap all of them
            return true;
            
        }
        
        public function clampIn(boundary : SpudCollider) : void
        {
            // Implement in derived classes
        }
        
        public function clampOff(barrier : SpudCollider) : void
        {
            // Implement in derived classes
        }
        
        public function clampOffList(barriers : Array) : void
        {
            var is_collider : Function = function(item : *, index : int, array : Array) : Boolean
            {
                if (item is SpudCollider)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            
            for each (var coll : SpudCollider in barriers.filter(is_collider))
            {
                clampOff(coll);
            }
            
        }
        
    }
    
}
